local mou__shipo = fk.CreateSkill {

  name = "joy_mou__shipo",

  tags = {  },

}



mou__shipo:addEffect(fk.EventPhaseStart, {
  name = "joy_mou__shipo",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(mou__shipo.name) and player.phase == Player.Finish then
      return table.find(player.room:getOtherPlayers(player), function (p) return p.hp < player.hp or p:hasDelayedTrick("supply_shortage") end)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets1 = table.filter(player.room.alive_players, function (p) return p.hp < player.hp end)
    local targets2 = table.filter(player.room:getOtherPlayers(player), function (p) return p:hasDelayedTrick("supply_shortage") end)
    local choices = {}
    if #targets1 > 0 then table.insert(choices, "mou__shipo_choice1") end
    if #targets2 > 0 then table.insert(choices, "mou__shipo_choice2") end
    local choice = room:askForChoice(player, choices, mou__shipo.name, "#mou__shipo-choose")
    local targets = {}
    if choice == "mou__shipo_choice2" then
      targets = targets2
    else
      local tos = room:askForChoosePlayers(player, table.map(targets1, Util.IdMapper), 1, 1, "#mou__shipo-choose", mou__shipo.name, false)
      targets = {room:getPlayerById(tos[1])}
    end
    for _, to in ipairs(targets) do
      if player.dead then break end
      local card = room:askForCard(to, 1, 1, false, mou__shipo.name, true, ".", "#mou__shipo-give::"..player.id)
      if #card > 0 then
        local get = card[1]
        room:obtainCard(player, get, false, fk.ReasonGive)
        if room:getCardArea(get) == Card.PlayerHand and room:getCardOwner(get) == player then
          local tos = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player), Util.IdMapper), 1, 1, "#mou__shipo-present:::"..Fk:getCardById(get):toLogString(), mou__shipo.name, true)
          if #tos > 0 then
            room:obtainCard(tos[1], get, false, fk.ReasonGive)
          end
        end
      else
        room:damage { from = player, to = to, damage = 1, skillName = mou__shipo.name }
      end
    end
  end,
})

return mou__shipo